/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.demo.widget.meis.slice;

import com.demo.widget.ResourceTable;
import com.meis.widget.utils.PixelUtil;
import com.meis.widget.xiaohongshu.tag.RandomDragTagLayout;
import com.meis.widget.xiaohongshu.tag.RandomDragTagView;
import com.meis.widget.xiaohongshu.tag.TagModel;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wenshi on 2019/3/14.
 * Description
 */
public class MeiRandomDragTagAbilitySlice extends AbilitySlice {
    RandomDragTagLayout mRandomDragTagLayout;
    private DependentLayout dependentLayout;
    private Image ivGirl;
    String mTagText = "仿小红书任意拖拽控件、欢迎关注公众号：控件人生";
    List<TagModel> mTagList = new ArrayList<>();
    private SecureRandom random = new SecureRandom();
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_mei_drag_tag_ability);
        PixelUtil.initContext(getContext());
        ivGirl = (Image)findComponentById(ResourceTable.Id_iv_girl);
        dependentLayout = (DependentLayout)findComponentById(ResourceTable.Id_DependentLayout);
        dependentLayout.setClipEnabled(false);
        mRandomDragTagLayout = (RandomDragTagLayout) findComponentById(ResourceTable.Id_tag_layout);
        mRandomDragTagLayout.setClipEnabled(false);
        findComponentById(ResourceTable.Id_tv_add).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                        float x= random.nextFloat();
                        float y = random.nextFloat();
                        boolean b= random.nextBoolean();
                mRandomDragTagLayout.addTagView(//mTagText.length()
                        mTagText.substring(0, random.nextInt(mTagText.length())),
                       x,ivGirl.getWidth(),
                        y,ivGirl.getHeight(),
                       b
                );
            }
        });

        findComponentById(ResourceTable.Id_tv_save).setClickedListener(new Component.ClickedListener()  {
            @Override
            public void onClick(Component v) {
                saveTag();
            }
        });

        findComponentById(ResourceTable.Id_tv_restore).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                restoreTag();
            }
        });
    }

    private void saveTag() {
        mTagList.clear();
        for (int i = 0; i < mRandomDragTagLayout.getChildCount(); i++) {
            Component childView = mRandomDragTagLayout.getComponentAt(i);
            if (childView instanceof RandomDragTagView) {
                RandomDragTagView tagView = (RandomDragTagView) childView;
                TagModel tagModel = new TagModel();
                tagModel.direction = tagView.isShowLeftView();
                tagModel.text = tagView.getTagText();
                tagModel.x = tagView.getPercentTransX();
                tagModel.y = tagView.getPercentTransY();
                mTagList.add(tagModel);
            }
        }
    }

    private void restoreTag() {
        if (!mTagList.isEmpty()) {
            mRandomDragTagLayout.removeAllComponents();
            for (TagModel tagModel : mTagList) {
                mRandomDragTagLayout.addTagView(
                        tagModel.text,
                        tagModel.x,
                        ivGirl.getWidth(),
                        tagModel.y,
                        ivGirl.getHeight(),
                        tagModel.direction);
            }
        }
    }
}
